<?php
$this->headLink()->appendStylesheet($this->baseUrl() . '/public/styles/colorbox/colorbox.css');
$this->headScript()->prependFile($this->baseUrl() . '/public/scripts/colorbox/jquery.colorbox-min.js');

$status = array('Pendiente', 'En Progreso', 'Pendiente con cliente', 'Cambios', 'Mas Información', 'Aprobado', 'Entregado');
?>
<script type="text/javascript">
    
    $(document).ready(function() {
        $("#users_copy").autocomplete({
            url: "<?php echo $this->baseUrl() ?>/orden/index/get-users-internal/",
            sortFunction: function(a, b, filter) {
                var f = filter.toLowerCase();
                var fl = f.length;
                var a1 = a.value.toLowerCase().substring(0, fl) == f ? '0' : '1';
                var a1 = a1 + String(a.data[0]).toLowerCase();
                var b1 = b.value.toLowerCase().substring(0, fl) == f ? '0' : '1';
                var b1 = b1 + String(b.data[0]).toLowerCase();
                if (a1 > b1) { return 1; }
                if (a1 < b1) { return -1;}
                return 0;
            },
            showResult: function(value, data) {
                return '<span style="color:white">' + value + '</span>';
            },
            onItemSelect: function(item) {
                var text = item.value;
                var ids;
                if (item.data.length) {
                    ids =  $("#id_users").val();
                    $("#id_users").val(ids + item.data.join(', ') +',');
                }
                $("#users_add ul").append('<li id="ui_'+item.data.join(', ')+'">'+ text +' <a class="delUser" href="#" onclick="return confirm (\'Esta seguro de quitar al usuario ? \')" rel="'+ item.data.join(', ') +'"><img src="<?php echo $this->baseUrl() ?>/public/styles/admin/images/icn_trash.png"/></a></li>');
                $("#users_copy").val('');
                $("#submit_copy").css('display', 'block');
            },
            mustMatch: true,
            maxItemsToShow: 5,
            selectFirst: false,
            autoFill: false,
            selectOnly: true,
            remoteDataType: 'json'
        });
        
        
        function validar_email(valor){
            // creamos nuestra regla con expresiones regulares.
            var filter = /[\w-\.]{3,}@([\w-]{2,}\.)*([\w-]{2,}\.)[\w-]{2,4}/;
            // utilizamos test para comprobar si el parametro valor cumple la regla
            if(filter.test(valor))
                return true;
            else
                return false;
        }
        
        $("#submit_copy").click(function(){
            if(validar_email($('#email_user_internal').val())){
                $.ajax({
                    url: '<?php echo $this->baseUrl() ?>/orden/index/add-copy-users/',
                    type: 'POST',
                    data: 'users='+$('#id_users').val()+"&orden_id="+<?php echo $this->details->id ?>+"&email="+$('#email_user_internal').val(),
                    success: function(data){
                        console.log(data);
                        alert('Un email con el detalle de los trabajos ha sido enviado a todos los usuarios.')
                    },
                    error: function(){
                        console.log('error');
                    }
                });
            }else{
                alert('Formato email incorrecto.');
            }
        });
        
        //Delete user
        $(document).on("click", "a.delUser", function(){ 
            idUser = $(this).attr('rel');
            $.ajax({
                type: "GET",
                url: "<?php echo $this->baseUrl() ?>/orden/index/delete-user-internal/id/"+idUser+"/idorden/"+<?php echo $this->details->id ?>,
                success: function( strData ){
                    if(strData == 'eliminado'){
                        $('#ui_'+idUser).remove();
                    }
                   
                }
            });
        });
    }); 
</script>
<article class="module width_full">
    <header><h3>Detalle de la orden</h3></header>
    <div class="module_content">
        <fieldset>
            <div style="float: left; width: 50%;">
                <table>
                    <tr>
                        <td><label>Codigo de Orden </label></td>
                        <td><span><?php echo $this->details->code ?></span></td>
                    </tr>
                    <tr>
                        <td><label>Titulo:</label></td>
                        <td><span><?php echo $this->details->title ?></span></td>
                    </tr>
                    <tr>
                        <td><label>Detalle de la Orden:</label></td>
                        <td><span><?php echo $this->details->description ?></span></td>
                    </tr>
                    <tr>
                        <td><label>Fecha de inicio</label></td>
                        <td><span><?php echo $this->details->create_date ?></span></td>
                    </tr>
                    <tr>
                        <td><label>Fecha de modificaci&oacute;n</label></td>
                        <td><span><?php echo $this->details->update_date ?></span></td>
                    </tr>
    <!--                <tr>
                        <td><label>Fecha limite</label></td>
                        <td><span><?php echo $this->details->end_date ?></span></td>
                    </tr>-->
                    <tr>
                        <td><label>Cambio Brief:</label></td>
                        <td><span><?php echo ($this->details->change_brief != '') ? $this->details->change_brief : 'No' ?></span></td>
                    </tr>
                    <tr>
                        <td><label>Trabajo Cambios</label></td>
                        <td><span><?php echo ($this->details->change_job != '') ? $this->details->change_job : 'No' ?></span></td>
                    </tr>
    <!--                <tr>
                        <td><label>Nueva Fecha Entrega</label></td>
                        <td><span><?php echo ($this->details->new_date_limit != '') ? $this->details->new_date_limit : 'No' ?></span></td>
                    </tr>-->
                </table>
                <br/>

                <table>
                    <tr>
                        <td><label>Trabajos a revisar</label></td>
                        <td>
                            <table border="1">
                                <tr>
                                    <td><b>Trabajo</b></td>
    <!--                                <td><b>Fecha Cliente</b></td>-->
                                    <td><b>Estado Actual</b></td>
                                    <td><b>Comentarios</b></td>
                                </tr>
    <!--                            <tr>
                                    <td style="width: 80px">
                                <?php echo $this->dataConcept->name ?>
                                    </td>
                                    <td style="width: 80px">
                                <?php echo $this->dataConcept->begin_date ?>
                                    </td>
                                    <td>
                                <?php echo $status[$this->dataConcept->status] ?>
                                    </td>
                                    <td>
                                        <a class="inline" href="<?php echo $this->url(array('module' => 'orden', 'controller' => 'index', 'action' => 'comment-job', 'id_orden' => $this->details->id, 'id_job' => 1, 'name' => 'Concepto')) ?>" title="Comentarios">
                                            <img src="<?php echo $this->baseUrl() ?>/public/styles/admin/images/comment.png" width="24"/> 
                                        </a>
                                    </td>
                                </tr>-->
                                <?php
                                foreach ($this->dataJob as $odj):
                                    if ($odj->id != 1):
                                        ?>
                                        <tr>
                                            <td style="width: 80px">
                                                <?php echo $odj->name ?>
                                            </td>
        <!--                                        <td style="width: 80px">
                                            <?php echo $odj->end_date_client ?>
                                            </td>-->
                                            <td>
                                                <?php echo $status[$odj->status] ?>
                                            </td>
                                            <td>
                                                <a class="inline" href="<?php echo $this->url(array('module' => 'orden', 'controller' => 'index', 'action' => 'comment-job', 'id_orden' => $this->details->id, 'id_job' => $odj->id, 'name' => $odj->name)) ?>" title="Comentarios">
                                                    <img src="<?php echo $this->baseUrl() ?>/public/styles/admin/images/comment.png" width="24"/> 
                                                </a>
                                            </td>
                                        </tr>
                                        <?php
                                    endif;
                                endforeach;
                                ?>
                            </table>
                        </td>
                    </tr>
                </table>
            </div>
            <!-- ESTA PARTE ES PARA COPIAR A LOS USUARIOS DE TIGO -->
            <div style="float: left; width: 40%;">
                <form name="formusers" id="formusers">
                    <label style="width: 250px;">Copiar detalle de los trabajos a:</label>
                    <input type="text" id="users_copy"/>
                    <br/>
                    <div id="users_add" style="margin-top: 50px">
                        <ul id="userslu">
                            <?php if ($this->resultUserInternal != null): ?>
                                <?php foreach ($this->resultUserInternal as $userInternal): ?>
                                    <li id="ui_<?php echo $userInternal->iduser ?>"><?php echo $userInternal->nombre . ' ' . $userInternal->apellidos; ?>
                                        <a class="delUser" onclick="return confirm ('Esta seguro de quitar al usuario ?')" rel="<?php echo $userInternal->iduser ?>">
                                            <img src="<?php echo $this->baseUrl() ?>/public/styles/admin/images/icn_trash.png"/>
                                        </a>
                                    </li>
                                    <?php $usersid .= $userInternal->iduser . ','; ?>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </ul>
                    </div>
                    <table>
                        <tr>
                            <td>Correo Electronico:</td>
                            <td><input type="text" name="email_user_internal" id="email_user_internal"/></td>
                            <td><i style="margin-left: 15px;">ex: nombre@correo.com</i></td>
                        </tr>
                    </table>
                    <input type="hidden" id="id_users" name="id_users" value="<?php echo $usersid ?>"/>
                    <?php if ($this->resultUserInternal != null): ?>
                        <input type="button" name="submit_copy" id="submit_copy" value="Copiar" style="margin-left: 10px"/>
                    <?php endif; ?>
                </form>
            </div>
        </fieldset>

        <article class="breadcrumbs" style="margin-top: 5px;">
            <a href="<?php echo $this->url(array('module' => 'attachment', 'action' => 'index', 'id' => $this->details->id)) ?>">Adjuntar archivos</a> 
            <div class="breadcrumb_divider"></div> 
            <a href="<?php echo $this->url(array('action' => 'index', 'id' => null)) ?>">Volver</a> 
        </article>
    </div>
    <footer></footer>
</article>

<script>
    $(".inline").colorbox({height:'450px'});
</script>
